<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>canvas画画</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <!-- <div id="canvas"></div>
    <script>
        canvas.onclick = (e) => {
            console.log(e.clientX)
            console.log(e.clientY)
            //console.log调试大法
            let div = document.createElement('div')
            div.style.position = 'absolute'
            div.style.left = e.clientX + 'px'
            div.style.top = e.clientY +'px'
            div.style.border ='1px solid '
            div.style.width = '6px'
            div.style.height = '6px'
            div.style.marginLeft = '-3px'
            div.style.marginTop = '-3px'
            div.style.borderRadius = '50%'
            div.style.backgroundColor = 'black'
            canvas.appendChild(div)
        }
    </script> -->
    <canvas id="canvas" width="100" height="100"></canvas>
    <!-- width与height是canvas的html的属性 -->
    <script>
        let canvas = document.getElementById("canvas");
        canvas.width = document.documentElement.clientWidth
        canvas.height = document.documentElement.clientHeight
        //画线
        let ctx = canvas.getContext("2d");
        ctx.fillStyle = "black";
        ctx.strokeStyle = "none";
        ctx.lineWidth = 10;
        ctx.lineCap = "round"
        
        // 画线函数
        function drawLine(x1,y1,x2,y2) {
            ctx.beginPath();
            ctx.moveTo(x1,y1);
            ctx.lineTo(x2,y2);
            ctx.stroke();
        }


        let painting = false
        let last

        var isTouchDevice = 'ontouchstart' in document.documentElement;
        //检测是否为触屏设备
        
        if (isTouchDevice) {
            canvas.ontouchstart = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                last = [x,y]
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                drawLine(last[0],last[1],x,y)
                last = [x,y]

            }
            //不使用drawline函数画线
            // canvas.ontouchmove = (e) => {
            //     let x = e.touches[0].clientX
            //     let y = e.touches[0].clientY
            //     ctx.beginPath();
            //     ctx.arc(x,y,10,0,2 * Math.PI)
            //     ctx.stroke();
            //     ctx.fill();
            // }
        } else {
            canvas.onmousedown = (e) => {
            painting = true 
            last = [e.clientX,e.clientY]
        }
        canvas.onmousemove = (e) => {
            if(painting === true){
                // 第一种画点圆
                // ctx.fillRect(e.clientX - 5,e.clientY - 5,10,10);
            //    第二种
                // ctx.beginPath();
                // ctx.arc(e.clientX,e.clientY,10,0,2 * Math.PI)
                // ctx.stroke();
                // ctx.fill();
                drawLine(last[0],last[1],e.clientX,e.clientY);
                last = [e.clientX,e.clientY];
            }
        }

        canvas.onmouseup = () => {
            painting = false
        }
        }







       

    </script>
</body>
</html>